// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'

import App from './App'
import router from './router'
import resource from 'vue-resource'

import element from 'element-ui'
import 'element-ui/lib/theme-default/index.css'

import './assets/css/app.less'

import test from '@/views/system/test'

Vue.config.productionTip = false

Vue.use(Vuex)
Vue.use(element)
Vue.use(resource)

Vue.http.options.emulateJSON = true

const store = new Vuex.Store({
  state: {
    tabs: [// 选项卡列表
      {
        code: 'home', // 选项卡 code 唯一标识
        name: '主页', // 选项卡名称
        template: test, // 选项卡要现实的组件
        closable: false // 选项卡是否可关闭
      }
    ],
    currentCode: 'home'
  },
  mutations: {
    change (state, code) {
      // 切合选项卡
      state.currentCode = code
    },
    open (state, view) {
      state.currentCode = view.code
      for (let item of state.tabs) {
        if (item.code === view.code) {
          return
        }
      }
      view.closable = true
      state.tabs.push(view)
    },
    close (state, code) {
      for (var i = 0; i < state.tabs.length; i++) {
        var element = state.tabs[i]
        if (element.code === code) {
          state.tabs.splice(i, 1)
          if (code === state.currentCode) {
            // 如果删除的是当前打开的选项卡，进行切换选项卡操作
            let current = state.tabs[i]
            if (current === undefined) {
              // 如果i位置没有选项卡，这说明被删除的是最后一个
              current = state.tabs[state.tabs.length - 1]
            }
            state.currentCode = current.code
          }
          return
        }
      }
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App, test }
})
